package com.sduept.nwld.dataview.controller.chart;

import com.sduept.bigdata.web.jsf.controller.AbstractController;
import com.sduept.core.dao.SuperDAO;
import org.primefaces.model.chart.Axis;
import org.primefaces.model.chart.AxisType;
import org.primefaces.model.chart.BarChartModel;
import org.primefaces.model.chart.ChartSeries;
import org.springframework.beans.factory.annotation.Autowired;

import javax.annotation.PostConstruct;
import javax.inject.Named;
import java.util.ArrayList;
import java.util.List;

@Named
public class FaultTypeChartView extends AbstractController {
	/**
	 * 
	 */
	private static final long serialVersionUID = 2035860103645809177L;
	
//	private List<LineFault> faults = new ArrayList<>();
	
//	private Map<String, List<LineFault>> reasonMap = new HashMap<String, List<LineFault>>();
	private List<?> reasonObjs = new ArrayList<>();
	private List<?> resoncountall = new ArrayList<>();
	
	@Autowired
	private SuperDAO dao;
	
	
	
	private BarChartModel barModel;
	private BarChartModel barAllHistoryModel;
	

	@PostConstruct
	public void init() {
//		String reason = getParameter("faultType");
		String reason = "雷电";
		
		String whereAll ="";
		String wherefault ="";
		if(reason != null){
			if("雷电".equals(reason)){
				whereAll = "where t.countType='all' and t.atype in ('大风','大风事件','暴雨','暴雨事件','雷电','雷雨大风')";
				wherefault ="where t.faultReson in ('雷击','风偏') group by t.faultReson ";
			}
		}
		resoncountall = dao.nativeQuery("select t.atype,t.counts from t_city_wf_weather_alert_count t "+whereAll);
		reasonObjs = dao.nativeQuery("select t.faultReson ,count(*) from t_cim_fault t "+wherefault);
		createBarModel();
		createtBarAllHistoryModel();
	}

	public BarChartModel getBarModel() {
		return barModel;
	}
	
	private void createtBarAllHistoryModel(){
		barAllHistoryModel = new BarChartModel();

		ChartSeries reasons = new ChartSeries();
		reasons.setLabel("气象类型");
		int maxy = 0;
		for(Object o : resoncountall){
			Object[] os = (Object[]) o;
			int y = Integer.valueOf(os[1].toString());
			reasons.set(os[0].toString(), y);
			maxy = maxy > y ? maxy:y;
		}
		barAllHistoryModel.addSeries(reasons);

		barAllHistoryModel.setTitle("气象类型统计");
		barAllHistoryModel.setLegendPosition("ne");

		Axis xAxis = barAllHistoryModel.getAxis(AxisType.X);
		xAxis.setLabel("类型");

		Axis yAxis = barAllHistoryModel.getAxis(AxisType.Y);
		yAxis.setLabel("发生次数");
		yAxis.setMin(0);
		yAxis.setMax(maxy+50);
	}

	private void createBarModel() {
		barModel = new BarChartModel();

		ChartSeries reasons = new ChartSeries();
		reasons.setLabel("故障原因");
		int maxy = 0;
		for(Object o : reasonObjs){
			Object[] os = (Object[]) o;
			int y = Integer.valueOf(os[1].toString());
			reasons.set(os[0].toString(), y);
			maxy = maxy > y ? maxy:y;
		}
		barModel.addSeries(reasons);

		barModel.setTitle("故障发生次数");
		barModel.setLegendPosition("ne");

		Axis xAxis = barModel.getAxis(AxisType.X);
		xAxis.setLabel("原因类型");

		Axis yAxis = barModel.getAxis(AxisType.Y);
		yAxis.setLabel("故障次数");
		yAxis.setMin(0);
		yAxis.setMax(maxy+50);
	}

	public BarChartModel getBarAllHistoryModel() {
		return barAllHistoryModel;
	}

}
